/**
 * 
 */
package com.myBase.core.facade.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.myBase.core.dao.GoldcoinActivitycashpoolDao;
import com.myBase.core.dao.GoldcoinDao;
import com.myBase.core.entity.GoldCoinForUserVo;
import com.myBase.core.entity.Goldcoin;
import com.myBase.core.entity.GoldcoinActivitycashpool;
import com.myBase.core.entity.GoldcoinRelateRules;
import com.myBase.core.entity.GoldcoinWapper;
import com.myBase.core.facade.service.EBaiGoldCoinInPutService;
import com.myBase.core.util.EBeiUtil;
import com.myBase.core.util.GoldCoinRatioUtils;
import com.myBase.common.persistence.Page;
import com.myBase.common.service.CrudAbstractService;

/**
 * 金币录入Service
 * @author 金币录入
 * @version 2017-05-15
 */
@Service("eBaiGoldCoinInPutService")
public class EBaiGoldCoinInPutServiceimpl extends CrudAbstractService<GoldcoinDao, Goldcoin>
        implements EBaiGoldCoinInPutService {

    @Autowired
    protected GoldcoinDao goldcoindao;

    @Autowired
    protected GoldcoinActivitycashpoolDao goldcoinActivitycashpoolDao;

    //------------------------------------------------------------------------------------------

    public Goldcoin get(String id) {
        return super.get(id);
    }

    public List<Goldcoin> findList(Goldcoin goldcoin) {
        return super.findList(goldcoin);
    }

    public Page<Goldcoin> findPage(Page<Goldcoin> page, Goldcoin goldcoin) {
        return super.findPage(page, goldcoin);
    }

    @Transactional(readOnly = false)
    public void save(Goldcoin goldcoin) {
        goldcoindao.insert(goldcoin);
    }

    @Transactional(readOnly = false)
    public void delete(Goldcoin goldcoin) {
        super.delete(goldcoin);
    }

    @Override
    public int ifOnlymark(String goldcoinmark) {
        return goldcoindao.ifOnlymark(goldcoinmark);
    }

    @Override
    public List<GoldcoinActivitycashpool> getgoldCoinActivity(String branchid) {
        return goldcoinActivitycashpoolDao.getgoldCoinActivity(branchid);
    }

    @Override
    public Page<GoldcoinWapper> findGoldCoinPage(Page<GoldcoinWapper> page, GoldcoinWapper goldcoinWapper) {
        goldcoinWapper.setPage(page);
        page.setList(goldcoindao.findGoldCoinPage(goldcoinWapper));
        return page;

    }

    @Override
    public String findOfficeCode(String userId) {
        String code = goldcoindao.findCode(userId);
        return code;
    }

    @Override
    public GoldcoinWapper getGoinDetail(String goldcoinid) {
        GoldcoinWapper goldcoinWapper = goldcoindao.getGoinDetail(goldcoinid);
        return goldcoinWapper;
    }

    @Override
    public Page<GoldcoinWapper> getNoDistribution(Page<GoldcoinWapper> page, GoldcoinWapper goldcoinWapper) {
        goldcoinWapper.setPage(page);
        page.setList(goldcoindao.getNoDistribution(goldcoinWapper));
        return page;
    }

    @Override
    public Goldcoin getGoinDetailedit(String goldcoinid) {
        Goldcoin goldcoin = goldcoindao.getGoinDetailedit(goldcoinid);
        return goldcoin;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public int checkGoldCoin(String id, String userId, Date date, String goldcoindistributiontotalnumber,
            String goldcoinactivity, String canusecash, String freezecash) throws Exception {
        int num = goldcoindao.checkGoldCoin(id, userId, date);
        BigDecimal cash = new BigDecimal(goldcoindistributiontotalnumber);
        BigDecimal a = cash.divide(GoldCoinRatioUtils.ratio, 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal b = new BigDecimal(canusecash.toString());
        BigDecimal c = new BigDecimal(freezecash.toString());
        BigDecimal add = c.add(a);
        BigDecimal subtract = b.subtract(a);
        logger.info("-------------这次冻结资金为：----------" + a);
        logger.info("-------------冻结资金总为：----------" + add);
        logger.info("-------------可用资金为：-----------" + subtract);
        Integer number = goldcoinActivitycashpoolDao.updateActivityCash(a, new Date(), goldcoinactivity);
        if (number != 1 || num != 1) {
            throw new Exception("更新金币状态或更新金币池资金失败");
        }
        return number;
    }

    @Override
    @Transactional(readOnly = false)
    public int checkBackGoldCoin(String id, String userId, Date date) {

        int num = goldcoindao.checkBackGoldCoin(id, userId, date);
        return num;
    }

    @Override
    public List<Goldcoin> goldCoinDistributeList(String branchid) {
        return goldcoindao.goldCoinDistributeList(branchid);
    }

    @Override
    public Integer goldCoinSumAjax(String goldCoinActivity, String branchId) {
        return goldcoindao.goldCoinSumAjax(goldCoinActivity, branchId);
    }

    @Override
    @Transactional(readOnly = false)
    public Integer goldCoinUpdate(Goldcoin goldcoin) {
        Integer num = goldcoindao.goldCoinUpdate(goldcoin);
        return num;
    }

    //从gold_coin表中获取需要结算的列表
    @Override
    public ArrayList<Goldcoin> getGoldCoinNeedSettlement() {
        ArrayList<Goldcoin> list = goldcoindao.getGoldCoinNeedSettlement();
        return list;
    }

    @Override
    @Transactional(readOnly = false)
    public Integer updateStstus(Integer id, String subChannel) {
        Integer num = goldcoindao.updateStstus(id, subChannel);
        return num;
    }

    @Override
    public Integer updateStatus(String status, Integer id) {
        Integer count = goldcoindao.updateStatus(status, id);
        return count;
    }

    @Override
    public Page<GoldcoinWapper> getDistribution(Page<GoldcoinWapper> page, GoldcoinWapper goldcoinWapper) {
        goldcoinWapper.setPage(page);
        page.setList(goldcoindao.getDistribution(goldcoinWapper));
        return page;

    }

    @Override
    public String getAddGoldCoinNumForUser(int goldcoinId, int ruleId) {

        logger.info("获取需要发金币数goldcoinId:" + goldcoinId + ",ruleId" + ruleId);
        GoldcoinRelateRules goldcoinRelateRules = goldcoindao.getGoldcoinRelateRulesForAndGold(goldcoinId, ruleId);
        String distributiostyle = goldcoinRelateRules.getDistributiostyle();
        if (distributiostyle.equals(EBeiUtil.GoldCorn_fixedDistributionNumber)) {
            String goldCoinNum = goldcoinRelateRules.getFixeddistributionnumber().toString();
            logger.info("获取需要发金币数为:" + goldCoinNum);
            return goldCoinNum;
        }
        if (distributiostyle.equals(EBeiUtil.GoldCorn_percent)) {
            //先不做，产品没想清楚。
        }
        if (distributiostyle.equals(EBeiUtil.GoldCorn_random)) {
            String random = goldcoinRelateRules.getRandom();
            String[] randoms = random.split(",");
            int last = Integer.valueOf(randoms[1]);
            int start = Integer.valueOf(randoms[0]);
            Random random1 = new Random();
            int a = random1.nextInt(last - start + 1) + start;
            logger.info("获取需要发金币数为:" + a);
            return String.valueOf(a);
        }
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public int goldCoinCancel(String id) {
        int num = goldcoindao.goldCoinCancel(id);
        return num;
    }

    @Override
    public Page<GoldCoinForUserVo> goldcoinforusers(Page<GoldCoinForUserVo> page, GoldCoinForUserVo goldCoinForUserVo) {
        goldCoinForUserVo.setPage(page);
        page.setList(goldcoindao.goldcoinforusers(goldCoinForUserVo));
        return page;
    }

    @Override
    @Transactional(readOnly = false)
    public int onlyUserCorn(String id) {
        int num = goldcoindao.onlyUserCorn(id);
        return num;
    }

}